--[[
    @Author       : baidwwy
    @Date         : 2020-09-22 19:49:01
    @LastEditTime : 2021-02-09 21:20:44
--]]
io.stdout:setvbuf('no',0)
local gge = package.loadlib("ggelua", "luaopen_ggelua")()--require("ggelua")
local entry,data,nstate = ...
gge.isdebug = data==nil
gge.entry = entry

local function 分割路径(path)
    local t,n = {},1
    for match in path:gmatch("([^;]+)") do
        t[n] = match
        n=n+1
    end
    return t
end

local lpath = "?.lua;脚本/?.lua;脚本/?/?.lua;!/脚本/?.lua;!/脚本/?/?.lua"
lpath = lpath:gsub('!',gge.getrunpath())
lpath_ = 分割路径(lpath)
local cpath = "?.dll;lib/?.dll;!/?.dll;!/lib/?.dll"
cpath = cpath:gsub('!',gge.getrunpath())
cpath = gge.utf8toansi(cpath)--转ansi

package.path = nil
package.cpath = nil
setmetatable(package, {
    __newindex=function (t,k,v)
        if k=='path' then
            lpath = v:lower():gsub('\\','/')
            lpath_ = 分割路径(lpath)
        elseif k=='cpath' then
            cpath = gge.utf8toansi(v)--转ansi
        else
            rawset(t, k, v)
        end
    end,
    __index = function (t,k)
        if k=='path' then
            return lpath
        elseif k=='cpath' then
            return gge.ansitoutf8(cpath)--转utf8
        end
    end
})

local m_unpack = require("cmsgpack.safe").unpack

if data then
    data = m_unpack(data)
end

local function 处理路径(path)
    -- local t = {}
    -- for match in path:gmatch("([^/]+)") do
    --     if match=='..' then
    --         table.remove(t)
    --     elseif match~='.' then
    --         table.insert(t, match)
    --     end
    -- end
    -- path = table.concat(t, "/")

    path = path:lower()--小写
    path = path:gsub('%.','/')
    path = path:gsub('\\','/')
    return path
end

local 读取文件,是否存在
if gge.isdebug then
    function 读取文件(path)
        local file<close> = io.open(gge.utf8toansi(path),'rb');
        if file then
            return file:read('a')
        end
    end

    function 是否存在(path)
        local file<close> = io.open(gge.utf8toansi(path),"rb")
        if file then
            return true
        end
    end
else
    function 是否存在(file)
        return data[file]
    end

    function 读取文件(file)
        return data[file]
    end

    function package.dir(path)
        local k,v
        return function ()
            repeat
                k,v = next(data, k)
                if k and k:find(path) then
                    return k,v
                end
            until not k
        end
    end
end

local function 搜索路径(path)
    for _,v in ipairs(lpath_) do
        local file = v:gsub('?',path)
        if 是否存在(file) then
            return file
        end
    end
end

table.insert(package.searchers, 1, function (path)
    path = 处理路径(path)
    path = 搜索路径(path)
    if path then
        return assert(load(读取文件(path),path))
    end
end)

function import(path,env,...)
    path = path:gsub('\\','/'):lower()
    local data = 读取文件(path)
    if data then
        return assert(load(data,path,"bt",env))(...)
    end
end

-- local searchpath = package.searchpath
-- function package.searchpath(a,b,...)
--     local r = searchpath(
--         gge.utf8toansi(a),
--         gge.utf8toansi(b),...)
--     if r then
--         return gge.ansitoutf8(r)
--     end
--     return nil
-- end

if not gge.isdebug then
    local success, res = pcall(require, entry)
    if not success then
        error(res,0)
    end
    return res
end

return require(entry)